/*
 * Featureous is distributed under the GPLv3 license.
 *
 * University of Southern Denmark, 2011
 */
package dk.sdu.mmmi.featureous.explorer.inspector;

import dk.sdu.mmmi.featureous.core.controller.Controller;
import dk.sdu.mmmi.featureous.core.model.ClassModel;
import dk.sdu.mmmi.featureous.core.model.SelectionManager;
import dk.sdu.mmmi.featureous.explorer.inspector.nodes.InspectorTraceNodeModel;
import dk.sdu.mmmi.featureous.core.model.TraceModel;
import dk.sdu.mmmi.featureous.core.model.SelectionChangeListener;
import dk.sdu.mmmi.featureous.core.ui.FeatureUI;
import dk.sdu.mmmi.featureous.core.ui.MethodUI;
import dk.sdu.mmmi.featureous.core.ui.PackageUI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import org.openide.nodes.Node;
import org.openide.util.LookupEvent;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.util.Lookup.Result;
import org.openide.util.LookupListener;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//dk.sdu.mmmi.featureous.explorer.inspector//TraceInspector//EN",
autostore = false)
@TopComponent.Description(preferredID = "Feature inspector",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "navigator", openAtStartup = false)
@ActionID(category = "Window", id = "dk.sdu.mmmi.featureous.core.FeatureInspector")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
public final class TraceInspectorTopComponent extends TopComponent implements ExplorerManager.Provider {

    private static TraceInspectorTopComponent instance;
    /** path to the icon used by the component and its open action */
    static final String ICON_PATH = "dk/sdu/mmmi/featureous/explorer/app_icon_small.png";
    private static final String PREFERRED_ID = "TraceInspectorTopComponent";
    private final ExplorerManager mgr = new ExplorerManager();
    private final Result<ClassModel> selectionC;
    private final Result<PackageUI> selectionP;
    private final Result<MethodUI> selectionM;
    private final ClassSel classSel;
    private final PkgSel pkgSel;
    private final MethodSel methodSel;

    public TraceInspectorTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(TraceInspectorTopComponent.class, "CTL_TraceInspectorTopComponent"));
        setToolTipText(NbBundle.getMessage(TraceInspectorTopComponent.class, "HINT_TraceInspectorTopComponent"));
        setIcon(ImageUtilities.loadImage(ICON_PATH, true));

        Lookup l = ExplorerUtils.createLookup(mgr, getActionMap());
        associateLookup(l);

        selectionC = this.getLookup().lookup(new Lookup.Template<ClassModel>(ClassModel.class));
        selectionP = this.getLookup().lookup(new Lookup.Template<PackageUI>(PackageUI.class));
        selectionM = this.getLookup().lookup(new Lookup.Template<MethodUI>(MethodUI.class));

        classSel = new ClassSel(selectionC);
        pkgSel = new PkgSel(selectionP);
        methodSel = new MethodSel(selectionM);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new BeanTreeView();

        setLayout(new java.awt.BorderLayout());
        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized TraceInspectorTopComponent getDefault() {
        if (instance == null) {
            instance = new TraceInspectorTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the TraceInspectorTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized TraceInspectorTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(TraceInspectorTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof TraceInspectorTopComponent) {
            return (TraceInspectorTopComponent) win;
        }
        Logger.getLogger(TraceInspectorTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        Controller.getInstance().getTraceSet().getSelectionManager().addSelectionListener(ecl);
        classSel.attach();
        pkgSel.attach();
        methodSel.attach();
    }

    @Override
    public void componentClosed() {
        classSel.detach();
        pkgSel.detach();
        methodSel.detach();
        Controller.getInstance().getTraceSet().getSelectionManager().removeSelectionListener(ecl);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        TraceInspectorTopComponent singleton = TraceInspectorTopComponent.getDefault();
        singleton.readPropertiesImpl(p);
        return singleton;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return mgr;
    }

    private void createTree(final List<TraceModel> selected) {
        AbstractNode root = new AbstractNode(new Children.Keys<TraceModel>() {

            @Override
            protected void addNotify() {
                if(selected!=null){
                    setKeys(selected);
                }else{
                    setKeys(new ArrayList<TraceModel>());
                }
            }

            @Override
            protected Node[] createNodes(TraceModel tm) {
                AbstractNode root = new AbstractNode(new InspectorTraceNodeModel(tm));
                root.setIconBaseWithExtension(new FeatureUI().getIconPath());
                root.setDisplayName(tm.getName());
                return new Node[]{root};
            }
        });

        mgr.setRootContext(root);
    }
    private SelectionChangeListener ecl = new SelectionChangeListener() {

        public void featureSelectionChanged(SelectionManager tl) {
            List<String> sels = new ArrayList<String>();
            sels.addAll(tl.getSelectedFeats());
            if (sels.size() > 0) {
                Collections.sort(sels);
                List<TraceModel> tms = new LinkedList<TraceModel>();
                for (String ss : sels) {
                    TraceModel tm = Controller.getInstance().getTraceSet().getFirstLevelTraceByName(ss);
                    if(tm!=null){
                        tms.add(tm);
                    }
                }
                createTree(tms);
            } else {
                AbstractNode root = new AbstractNode(Children.LEAF);
                mgr.setRootContext(root);
            }
        }

        public void compUnitSelectionChanged(SelectionManager tl) {
        }

    };
}

abstract class LookupSelectionListener<T> implements LookupListener {

    protected Result<T> res;

    public Set<T> getSelectedClasses() {
        Set<T> m = new HashSet<T>(res.allInstances());
        return m;
    }

    public void attach(){
        res.addLookupListener(this);
        res.allItems();
    }

    public void detach(){
        res.removeLookupListener(this);
    }

    abstract public void resultChanged(LookupEvent le);

}

class ClassSel extends LookupSelectionListener<ClassModel> {

    public ClassSel(Result<ClassModel> res) {
        this.res = res;
    }

    @Override
    public void resultChanged(LookupEvent le) {
        Set<ClassModel> tmc = getSelectedClasses();
        Set<String> classes = new HashSet<String>();
        for (ClassModel cm : tmc) {
            classes.add(cm.getName());
        }

        Controller.getInstance().getTraceSet().getSelectionManager().getSelectedClasses().clear();
        Controller.getInstance().getTraceSet().getSelectionManager().addClassSelection(classes);
    }
}
class PkgSel extends LookupSelectionListener<PackageUI> {

    public PkgSel(Result<PackageUI> res) {
        this.res = res;
    }

    @Override
    public void resultChanged(LookupEvent le) {
        Set<PackageUI> tmc = getSelectedClasses();
        Set<String> pkgs = new HashSet<String>();
        for (PackageUI cm : tmc) {
            pkgs.add(cm.getName());
        }

        Controller.getInstance().getTraceSet().getSelectionManager().getSelectedPkgs().clear();
        Controller.getInstance().getTraceSet().getSelectionManager().addPkgSelection(pkgs);
    }
}
class MethodSel extends LookupSelectionListener<MethodUI> {

    public MethodSel(Result<MethodUI> res) {
        this.res = res;
    }

    @Override
    public void resultChanged(LookupEvent le) {
        Set<MethodUI> tmc = getSelectedClasses();
        Set<String> methods = new HashSet<String>();
        for (MethodUI cm : tmc) {
            methods.add(cm.getName());
        }

        Controller.getInstance().getTraceSet().getSelectionManager().getSelectedExecs().clear();
        Controller.getInstance().getTraceSet().getSelectionManager().addExecSelection(methods);
    }
}
